//
//  PerformanceTimeFilterAlertVM.swift
//  kxf
//
//  Created by Mac on 2024/3/24.
//

import Foundation
import UIKit
import MCToast

class PerformanceTimeFilterAlertVM: UIView {
    
    var controller = WHBaseViewVC()
    var resetBlock:(()->())?
    var queryBlock:((String,String)->())?
    
    var type = "day"
    
    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDHT, height: SCREEN_HEIGHT))
        self.backgroundColor = WHColorWithAlpha(colorStr: "000000", alpha: 0.66)
        self.isUserInteractionEnabled = true
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(hiddenSelfAciton))
        self.addGestureRecognizer(tap)
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    lazy var whiteView : UIView = {
        let vi = UIView.init(frame: CGRect.init(x: 0, y: WHBaseViewVC().getNavigationBarHeight(), width: SCREEN_WIDHT, height: kFitWidth(186)))
        vi.backgroundColor = .white
        vi.isUserInteractionEnabled = true
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(nothingToDoAction))
        vi.addGestureRecognizer(tap)
        
        return vi
    }()
    lazy var timeTitleLabel : UILabel = {
        let lab = UILabel.init(frame: CGRect.init(x: kFitWidth(16), y: kFitWidth(12), width: kFitWidth(100), height: kFitWidth(22)))
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 16, weight: .medium)
        lab.text = "选择日期"
        
        return lab
    }()
    lazy var startTimeBtn : TradeListAlertTimeButton = {
        let btn = TradeListAlertTimeButton.init(frame: CGRect.init(x: kFitWidth(16), y: kFitWidth(46), width: kFitWidth(152), height: kFitWidth(48)))
//        btn.timeLabel.text = "开始时间"
        btn.timeLabel.text = Date().getLast30Days()
        btn.timeString = Date().getLast30Days()
        
        return btn
    }()
    lazy var endTimeBtn : TradeListAlertTimeButton = {
        let btn = TradeListAlertTimeButton.init(frame: CGRect.init(x: kFitWidth(207), y: kFitWidth(46), width: kFitWidth(152), height: kFitWidth(48)))
//        btn.timeLabel.text = "结束时间"
        return btn
    }()
    
    lazy var resetBtn : UIButton = {
        let btn = UIButton.init(frame: CGRect.init(x: kFitWidth(58), y: kFitWidth(122), width: kFitWidth(101), height: kFitWidth(48)))
        btn.setTitle("重置", for: .normal)
        btn.setTitleColor(.COLOR_TEXT_BLACK333, for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .regular)
        btn.layer.cornerRadius = kFitWidth(4)
        btn.clipsToBounds = true
        btn.backgroundColor = WHColor_16(colorStr: "F4F4F4")
        btn.addTarget(self, action: #selector(resetAction), for: .touchUpInside)
        return btn
    }()
    lazy var confirmBtn : UIButton = {
        let btn = UIButton.init(frame: CGRect.init(x: kFitWidth(198), y: kFitWidth(122), width: kFitWidth(101), height: kFitWidth(48)))
        btn.setTitle("确定", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .regular)
        btn.layer.cornerRadius = kFitWidth(4)
        btn.clipsToBounds = true
        btn.backgroundColor = .THEME
        
        btn.addTarget(self, action: #selector(confirmAction), for: .touchUpInside)
        return btn
    }()
}

extension PerformanceTimeFilterAlertVM{
    @objc func nothingToDoAction() {
        
    }
    @objc func resetAction() {
        if type == "day"{
            self.startTimeBtn.timeLabel.text = "\(Date().getLast30Days())"
            self.endTimeBtn.timeLabel.text = "\(Date().todayDate)"
        }else{
            self.startTimeBtn.timeLabel.text = "\((Date().getLast30Days().mc_clipFromPrefix(to: 7)))"
            self.endTimeBtn.timeLabel.text = "\((Date().todayDate.mc_clipFromPrefix(to: 7)))"
        }
        if self.resetBlock != nil{
            self.resetBlock!()
        }
    }
    @objc func confirmAction() {
        var formatter = type == "month" ? "yyyy-MM" : "yyyy-MM-dd"
        if !Date().judgeDateIsMin(minDate: self.startTimeBtn.timeLabel.text!, maxDate: self.endTimeBtn.timeLabel.text!,format: formatter){
            MCToast.mc_text("开始时间不能大于结束时间")
            return
        }
        
        if self.queryBlock != nil{
            self.queryBlock!(self.startTimeBtn.timeLabel.text ?? "",self.endTimeBtn.timeLabel.text ?? "")
        }
        self.isHidden = true
    }
    @objc func hiddenSelfAciton() {
        self.isHidden = true
    }
}

extension PerformanceTimeFilterAlertVM{
    func initUI() {
        addSubview(whiteView)
        whiteView.addSubview(timeTitleLabel)
        whiteView.addSubview(startTimeBtn)
        whiteView.addSubview(endTimeBtn)
        whiteView.addSubview(resetBtn)
        whiteView.addSubview(confirmBtn)
    }
}
